/********************** DESCRIPTION***********************************
This code reproduces Figure 5 and Table A2 in the paper 
"Reverting to Informality: Unregistered Property Transactions 
and the Erosion of the Titling Reform in Peru"
by Gutierrez and Molina 
**********************************************************************/

clear
set more off
cap log close
log using "$datadir\Figure5.smcl", replace

global results "C:\GutierrezMolina"
use "$results\tempdata.dta", clear


*Code if for each year there has been a transaction at the household level
forvalues y=2000/2010 {
gen acto`y'=seg_acto_year==`y'
by i, sort: egen maxacto`y'=max(acto`y')
}

*Keep one obsevation per household
by i, sort: gen firstobs=_n==1
keep if firstobs==1
drop firstobs

egen maxacto=rowmax( maxacto2000 maxacto2001 maxacto2002 maxacto2003 maxacto2004 maxacto2005 maxacto2006 maxacto2007 maxacto2008 maxacto2009 maxacto2010)
tab maxacto
cap drop maxacto

*Reshape data to long format
reshape long maxacto , i(i) j(year)

*Gen period after policy change
ge after_t=0
replace after_t=1 if year>=2004

gen year2=year-2003
ge treated_t=area1*after_t

cap drop filter

*Gen district IDs
tostring distrito, gen(distrito_string)
tostring a3, gen(a3_string)

gen l_distrito=length(distrito_string)
replace distrito_string="0"+distrito_string if l_distrito==5

gen l_a3=length(a3_string)
replace a3_string="0"+a3_string if l_a3==5

gen IDDIST=distrito_string if area1==1
replace IDDIST=a3_string if area1==0

************************************************
*Figure 5. Average probability of having property transactions 
************************************************
table year area1, c(mean maxacto)
bysort year area1: egen meanprobacto=mean(maxacto)
bysort year area1: gen firstobs=_n==1

gen Tmeanprobacto=meanprobacto if area1==1
label var Tmeanprobacto "Treated group"
gen Cmeanprobacto=meanprobacto if area1==0
label var Cmeanprobacto "Control group"

#delimit ;
twoway (connected Cmeanprobacto year if firstobs==1 & area1==0 & year>=2000 & year<=2010, sort lcolor(gray) lpattern(dash) mcolor(gray)) 
(connected Tmeanprobacto year if firstobs==1 & area1==1 & year>=2000 & year<=2010, sort lcolor(black) lpattern(solid) mcolor(black)), 
ytitle(Probability of a transaction) xtitle(Year) xline(2004, lwidth(vvvthin) lcolor(black) lpattern(solid)) 
legend(fcolor(none) lcolor(none) region(lcolor(none))) scheme(s1mono);
#delimit cr;
graph export "$results\Figure5.pdf", as(pdf) replace
graph export "$results\Figure5.png", as(png) replace
graph export "$results\Figure5.eps", as(eps) replace

****************************************************************************************************
*Tabe A2 in Online Appendix. Effect of eliminating the RPU on the incidence of property transactions 
*****************************************************************************************************

#delimit ;
global X "
age_head female secondary postsecondary educlevel_head_missing married
hh_size lincome_family_pc
invasion lot_size time_habita
coast altitud idh alfabetismo";
#delimit cr;

global cluster "IDDIST"

reg maxacto after_t area1 treated_t $X year if year>=2000 & year<=2010 , cluster(${cluster})
outreg2 after_t area1 treated_t using "$results\incidence", excel replace

cap log close

exit

